﻿/*
If you need anymore help with your modifications to JacOS you can contact me
(Jacob Misirian) in person or Email me at misirianrule@gmail.com
*/



using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Sys = Cosmos.System;
using Cosmos;
using JacOS.Programs;



namespace JacOS
{

    /*
        This is where we define variable that can be used 
        anytime, anywhere in our code. To refrence ne of these 
        global variables you must use MyGlobals.x for example. But 
        local variables (ones that can only be used inside of their
        function (example: protected override void Run()) you can
        just use like you would any variable.
    */

    public static class MyGlobals //Most of these get changed, they are just defaults
    {
        public static string input;
        public static string version = "2.0.3";//The variable MyGlobals.version should be called for refrance of the current version for simplicity
        public static string user = "guest";//Default login
        public static string Path = "D:\\";//Set the default path as D:\
        public static int current_level = 1; //Set default level to that of guest. This is used for checking level permissions
        public static string history = "";
        public static string last = "";
        public static string savetxt = "";
        public static bool isinbas = false;
        public static string command = "";
        public static string text = "";
        public static string textSVG = "";
    }

    public static class Variables
    {
        public static string uinput = "";
        public static string a = "";
        public static string b = "";
        public static string c = "";
        public static string d = "";
        public static string e = "";
        public static string f = "";
        public static string g = "";
        public static string h = "";
        public static string i = "";
        public static string j = "";
        public static string k = "";
        public static string l = "";
        public static string m = "";
        public static string n = "";
        public static string o = "";
        public static string p = "";
        public static string q = "";
        public static string r = "";
        public static string s = "";
        public static string t = "";
        public static string u = "";
        public static string v = "";
        public static string w = "";
        public static string x = "";
        public static string y = "";
        public static string z = "";
    }


    public class Kernel : Sys.Kernel
    {
        //These are here instead of in their own class because they use the BeforeRun and Run methods which are protected overrides


        public void batch(bool svg)
        {
            if (svg == false)
            {
            Prompt:
                Console.Write("\nWould you like to see batch help (recomended) y/n >>  ");
                var ShowHelp = Console.ReadLine();
                if (ShowHelp == "y")
                {
                    Console.Write(Commands.batch.helpdoc);
                }
                else if (ShowHelp == "n")
                {
                    //Procede
                }
                else
                {
                    goto Prompt;
                }

                MyGlobals.command = "";
                string line;
                int count = 1;
                while (true)
                {
                    Console.Write(count + ">");
                    line = Console.ReadLine();
                    count = count + 1;
                    if (line == "end")
                    {
                        goto run;
                    }
                    if (line == "save")
                    {
                        string basSVG = MyGlobals.command;
                        break;
                    }
                    MyGlobals.command = MyGlobals.command + ("\n" + line);
                }

                goto run;

            }

            MyGlobals.isinbas = true;

        run:
            String[] cmds = MyGlobals.command.Split('\n');
            foreach (string cmd in cmds)
            {
                MyGlobals.input = cmd;
                Run();
            }


            MyGlobals.isinbas = false;
        }


        public void restart()
        {
            clrscreen.run();
            MyGlobals.user = users.Users.guest;
            MyGlobals.Path = "D:\\";
            BeforeRun();
        }



        //--------------------------------------------------------------------------------------------------



        //This code is run once when the OS boots up        
        protected override void BeforeRun()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.BackgroundColor = ConsoleColor.Blue;

            //Display welcome message
            Console.Write("Welcome to JacOS version " + MyGlobals.version + "\nYou are logged on by default as Guest\nFor a list of commands type help\nFor help with a specific command type  helpdocs [command]");

            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;
        DoSettings:
            Console.Write("\nWould you like to set up settings for this session\nor continue with default? y/n >> ");

            var YesNoTEMP = Console.ReadLine();
            if (YesNoTEMP == "y")
            {
                settings.run();
            }
            else if (YesNoTEMP == "n")
            {
                //Do nothing
            }
            else
            {
                goto DoSettings;
            }
        }




        //--------------------------------------------------------------------------------------------







        //This code is executed over and over again in a loop. This is where the main code takes place.
        protected override void Run()
        {
            //This is so that when a Batch file is being run it won't display the prompt and just run the commands
            if (MyGlobals.isinbas == false)
            {
                //Diplay the prompt
                Console.Write("\n" + MyGlobals.Path + "$ ");

                //Get string MyGlobals.input from the user
                MyGlobals.input = Console.ReadLine();
            }

            /*
            All of the if and if else statements are the differant
            commands that that can be processed based on string MyGlobals.input
            */
        Detect:
            if (MyGlobals.input == "last")
            {
                /*
                 I didn't use a custom function here because the labels
                 are not global and connot refrence each other from other
                 functions even though those functions are public. It doesn't
                 effect the way it executes at all but if you have OCD there
                 might be some issues while staring at the code EDIT: As a general
                 rule you should never use labels and gotos unless it's absolutely nessescary
                */
                MyGlobals.input = MyGlobals.last;
                goto Detect;
            }
            else if (MyGlobals.input == "log")
            {
                log.run();
            }
            else if (MyGlobals.input == "help")
            {
                help.run();
            }
            else if (JacOS.seperate(MyGlobals.input, 4) == "echo")
            {
                if (MyGlobals.input == "echo")
                {
                    Console.Write(Commands.echo.usage);
                }
                else
                {
                    var arg = JacOS.rseperate(MyGlobals.input, 5, (MyGlobals.input.Length - 1));
                    arg = JacOS.GatherArgs(arg);
                    echo.run(arg);
                }
            }
            else if (JacOS.seperate(MyGlobals.input, 2) == "cd")
            {
                if (MyGlobals.input == "cd")
                {
                    cd.run();
                }
                else
                {
                    var arg = JacOS.rseperate(MyGlobals.input, 3, (MyGlobals.input.Length - 1));
                    arg = JacOS.GatherArgs(arg);
                    cd.run(arg);
                }
            }
            else if (MyGlobals.input == "info")
            {
                info.run();
            }
            else if (MyGlobals.input == "friend")
            {
                friend.run();
            }
            else if (MyGlobals.input == "whoami")
            {
                whoami.run();
            }
            else if (JacOS.seperate(MyGlobals.input, 5) == "login")
            {
                if (MyGlobals.input == "login")
                {
                    Console.Write(Commands.login.usage);
                }
                else
                {
                    var arg = JacOS.rseperate(MyGlobals.input, 6, (MyGlobals.input.Length - 1));
                    arg = JacOS.GatherArgs(arg);
                    login.run(arg);
                }
            }
            else if (MyGlobals.input == "accounts")
            {
                
            }
            else if (MyGlobals.input == "myinfo")
            {
                myinfo.run();
            }
            else if (MyGlobals.input == "clrlog")
            {
                clrlog.run();
            }
            else if (JacOS.seperate(MyGlobals.input, 9) == "writeline")
            {
                if (MyGlobals.input == "writeline")
                {
                    Console.Write(Commands.writeline.usage);
                }
                else
                {
                    var arg = JacOS.rseperate(MyGlobals.input, 10, (MyGlobals.input.Length - 1));
                    arg = JacOS.GatherArgs(arg);
                    writeline.run(JacOS.ParseNum(arg, 0, 1000, 0));
                }

            }
            else if (MyGlobals.input == "shutdown")
            {
                shutdown.run();
            }
            else if (MyGlobals.input == "restart")
            {
                restart();
            }
            else if (MyGlobals.input == "math")
            {
                math.run();
            }
            else if (JacOS.seperate(MyGlobals.input, 8) == "helpdocs")
            {
                if (MyGlobals.input == "helpdocs")
                {
                    Console.Write(Commands.helpdocs.usage);
                }
                else
                {
                    var arg = JacOS.rseperate(MyGlobals.input, 9, (MyGlobals.input.Length - 1));
                    JacOS.GatherArgs(arg);
                    helpdocs.run(arg);
                }
            }
            else if (MyGlobals.input == "settings")
            {
                settings.run();
            }
            else if (JacOS.seperate(MyGlobals.input, 4) == "copy")
            {
                if (MyGlobals.input == "copy")
                {
                    Console.Write(Commands.copy.usage);
                }
                else
                {
                    var arg = JacOS.rseperate(MyGlobals.input, 5, (MyGlobals.input.Length - 1));
                    arg = JacOS.GatherArgs(arg);
                    copy.run(arg);
                }
            }
            else if (MyGlobals.input == "paste")
            {
                paste.run();
            }
            else if (MyGlobals.input == "batch")
            {
                batch(false);
            }
            else if (MyGlobals.input == "batchSVG")
            {
                batch(true);
            }
            else if (MyGlobals.input == "edit")
            {
                edit.run();
            }
            else if (MyGlobals.input == "editSVG")
            {
                editSVG.run();
            }
            else if (JacOS.seperate(MyGlobals.input, 5) == "pause")
            {
                if (MyGlobals.input == "pause")
                {
                    pause.run();
                }
                else
                {
                    var arg = JacOS.rseperate(MyGlobals.input, 6, (MyGlobals.input.Length - 1));
                    arg = JacOS.GatherArgs(arg);
                    pause.run(arg);
                }
            }
            else if (MyGlobals.input == "clrscreen")
            {
                clrscreen.run();
            }
            else if (JacOS.seperate(MyGlobals.input, 5) == "input")
            {
                if (MyGlobals.input == "input")
                {
                    Console.Write(Commands.input.usage);
                }
                else
                {
                    var arg = JacOS.rseperate(MyGlobals.input, 6, (MyGlobals.input.Length - 1));
                    input.run(arg);
                }

            }
            else if (MyGlobals.input == "")
            {
                //Do nothing
            }


            /*
               Every time you want to add functionality for a new command do an 
               if else statement as shown above. DO NOT add an if else statement 
               after the else statement, it will generate compile errors
            */
            //This is if the user has entered text into the prompt but the text is not a valid command.
            else
            {
                Console.Write(MyGlobals.input + " is not recognised as a valid command, for help type help.");
            }


            //This adds to the logs and remembers the command for use of the last command
            MyGlobals.history = MyGlobals.history + (MyGlobals.input + "  ");
            MyGlobals.last = MyGlobals.input;
        }


    }


}